package com.huika.dealontop.control.homepage.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.support.domain.DindanBean;

import java.util.ArrayList;

/**
 * Created by cailu on 2015/12/1.
 */
public class DindanDbHelper {

    private SQLiteDatabase mReadableDatabase;
    private static DindanDbHelper mDindanDbHelper;


    public DindanDbHelper() {
        if (mReadableDatabase == null) {
            mReadableDatabase = GlobalApp.getDindanDb().getReadableDatabase();
        }
    }

    public static DindanDbHelper getInstance() {
        if (mDindanDbHelper == null) {
            mDindanDbHelper = new DindanDbHelper();
        }
        if (!mDindanDbHelper.mReadableDatabase.isOpen()) {
            mDindanDbHelper.mReadableDatabase = GlobalApp.getDindanDb().getReadableDatabase();
        }
        return mDindanDbHelper;
    }

    /**
     * 设置所有的内容
     *
     * @param time
     * @param imageurl
     * @param content
     * @param id
     * @param isslected
     * @return
     */
    public long setContent(long time, String imageurl, String content, String id, int isslected) throws CloneNotSupportedException {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DindanDB.TIME, time);
        contentValues.put(DindanDB.IMAGEURL, imageurl);
        contentValues.put(DindanDB.CONTENT, content);
        contentValues.put(DindanDB.PROJECT_ID, id);
        contentValues.put(DindanDB.ISSELECTED, isslected);
        long insert = mReadableDatabase.insert(DindanDB.DINDANTB, null, contentValues);
        mReadableDatabase.close();
        return insert;
    }

    /**
     * 查询所有的条目
     *
     * @return
     */
    public int getCountNum() {
        String sql = "select project_id from dindan_tb";

        Cursor cursor = mReadableDatabase.rawQuery(sql, null);
        int size = cursor.getCount();
        cursor.close();
        mReadableDatabase.close();
        return size;
    }

    /**
     * 查询没有查看的条目
     *
     * @return
     */
    public int getUnUsedableCountNum() {
        ArrayList<DindanBean> dindanBeans = queryContent(Integer.MAX_VALUE);
        int count = 0;
        for (DindanBean db : dindanBeans) {
            //没有阅读的统计.
            if (db.isSelected()) {
                count++;
            }
        }
        ;
        return count;
    }


    /**
     * 将所有内容放进到bean中,然后将所有的bean放进一个集合中.
     *
     * @return
     */
    public ArrayList<DindanBean> queryContent(int limit) {
        String sql = "select time,imageurl,content,project_id,isselected from dindan_tb order by _id desc limit " + limit + " offset 0";
        ArrayList<DindanBean> al = new ArrayList<>();
        Cursor cursor = mReadableDatabase.rawQuery(sql, null);
        int size = cursor.getCount();
        if (cursor != null) {
            //得到 列明称
            String[] curName = cursor.getColumnNames();

            while (cursor.moveToNext()) {
                long time = cursor.getLong(cursor.getColumnIndex(curName[0]));
                String imageurl = cursor.getString(cursor.getColumnIndex(curName[1]));
                String content = cursor.getString(cursor.getColumnIndex(curName[2]));
                String project_id = cursor.getString(cursor.getColumnIndex(curName[3]));
                int selected = cursor.getInt(cursor.getColumnIndex(curName[4]));
                DindanBean dindanBean = new DindanBean();
                dindanBean.setTimes(time);
                dindanBean.setProductImageUrl(imageurl);
                dindanBean.setProductName(content);
                dindanBean.setProductId(project_id);
                //1代表有消息阅读了,0代表没有消息阅读
                if (selected == 1) {
                    dindanBean.setSelected(true);
                } else {
                    dindanBean.setSelected(false);
                }

                al.add(dindanBean);
            }
        }
        cursor.close();
        mReadableDatabase.close();
        return al;
    }

    /**
     * @param parameter 修改条件的参数
     * @param value     需要修改的字段及内容
     */
    public  void upDateSelected(String parameter, boolean value) {
        ContentValues cv = new ContentValues();//实例化ContentValues
        cv.put("isselected", value);//添加要更改的字段及内容
        String whereClause = "time=?";//修改条件
        String[] whereArgs = {parameter};//修改条件的参数
        mReadableDatabase.update(DindanDB.DINDANTB, cv, whereClause, whereArgs);//执行修改
        mReadableDatabase.close();

    }

    public int deleteDate(long time){

        String[] args = {String.valueOf(time)};
        return mReadableDatabase.delete("dindan_tb", "time=?", args);
    }

}
